package com.lap.framework.common.exception;

import com.lap.framework.common.dto.result.ErrorCode;
import lombok.Getter;
import org.apache.commons.lang3.ArrayUtils;

/**
 * 业务异常中断，系统异常中断参考: {@link SystemException}
 *
 * <ol>
 *   <li>创建参考: {@link #interrupt(ErrorCode)}
 *   <li>创建参考: {@link #interrupt(ErrorCode, Object...)}
 *   <li>创建参考: {@link #newBiz(ErrorCode)}
 *   <li>创建参考: {@link #newBiz(ErrorCode, Object...)}
 * </ol>
 *
 * @author Shuisheng Lao(劳水生)
 * @version 0.0.1
 */
@Getter
public final class BizException extends RuntimeException {

  private final transient ErrorCode errorCode;
  private final transient Object[] args;

  private BizException(ErrorCode errorCode, Object... args) {
    super(errorCode.getInternalCode());
    this.errorCode = errorCode;
    this.args = args;
  }

  public static void interrupt(ErrorCode errorCode) {
    interrupt(errorCode, ArrayUtils.EMPTY_OBJECT_ARRAY);
  }

  public static void interrupt(ErrorCode errorCode, Object... args) {
    throw new BizException(errorCode, args);
  }

  public static BizException newBiz(ErrorCode errorCode) {
    return newBiz(errorCode, ArrayUtils.EMPTY_OBJECT_ARRAY);
  }

  public static BizException newBiz(ErrorCode errorCode, Object... args) {
    return new BizException(errorCode, args);
  }
}
